﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ttlang
{
    public static class Builders
    {
        static Dictionary<builder.BuildTarget, builder.IBuilder> builders
            = new Dictionary<builder.BuildTarget, builder.IBuilder>();
        static Builders()
        {
            RegBuilder(new builder.builder_c());
            RegBuilder(new builder.builder_cs());
            RegBuilder(new builder.builder_ts());

        }
        static void RegBuilder(builder.IBuilder builder)
        {
            builders[builder.type] = builder;
        }
        public static builder.IBuilder GetBuilder(builder.BuildTarget target)
        {
            return builders[target];
        }
        static string ConvertPath(ProjectOption option, string path, bool clear = false)
        {
            var name = option.projectName;
            path = path.Replace("$PROJECTNAME", name);
            var outpath = System.IO.Path.GetFullPath(path, option.proiectPath);
            if (clear)
            {
                if (System.IO.Directory.Exists(outpath))
                {
                    try
                    {
                        System.IO.Directory.Delete(outpath, true);
                    }
                    catch
                    {

                    }
                }
            }
            if (System.IO.Directory.Exists(outpath) == false)
                System.IO.Directory.CreateDirectory(outpath);
            return outpath;
        }
        public static builder.ProjectResult BuildProject(compiler.CompilerContext context)
        {
            var option = context.projectOption;
            var outputs = context.projectOption.compilerOptions.outputs;
            //Console.WriteLine("outputs : \t"+ outputs);
            if (outputs != null)
            {
                builder.ProjectResult r = new builder.ProjectResult();
                r.targets = new builder.Result[outputs.Count];
                r.targetcount = outputs.Count;
                r.succcount = 0;
                Console.WriteLine(r.targetcount);
                for (var i = 0; i < outputs.Count; i++)
                {
                    Console.WriteLine("==output=>" + outputs[i].target);
                    var buildertype = Enum.Parse<builder.BuildTarget>(outputs[i].target);
                    var _builder = Builders.GetBuilder(buildertype);
                    //Console.WriteLine(_builder);
                    r.targets[i] = _builder.BuildModule(context.module,
                        ConvertPath(option, outputs[i].outPath, true),
                        ConvertPath(option, outputs[i].tempPath, true),
                        option.type,
                        outputs[i].autoBuild,
                        outputs[i].targetOptions
                        );
                    if (r.targets[i].state == builder.BuildState.SUCC)
                        r.succcount++;
                }
                return r;
            }
            return null;

        }
    }
}

